﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPF_DataLogger
{
    /// <summary>
    /// Interaction logic for GPSLocation.xaml
    /// </summary>
    public partial class GPSLocation : Window
    {
        private string _longitude;
        private string _latitude;

        public GPSLocation(ref string longitude, ref string latitude)
        {
            InitializeComponent();
            UserComponentsInit(ref longitude, ref latitude);
        }

        private void UserComponentsInit(ref string longitude, ref string latitude)
        {
            _longitude = longitude;
            _latitude = latitude;
            LongitudeBox.Text = _longitude;
            LatitudeBox.Text = _latitude;
        }

        private void OKBtn_Click(object sender, RoutedEventArgs e)
        {
            if (LongitudeBox.Text != "")
            {
                string Str = LongitudeBox.Text.Trim();
                
                double Num;

                bool isNum = double.TryParse(Str, out Num);

                if (!isNum)
                {
                    MessageBox.Show("Invalid number");
                    return;
                }

                _longitude = LongitudeBox.Text;
            }

            if (LatitudeBox.Text != "")
            {
                string LatStr = LatitudeBox.Text.Trim();

                double LatNum;

                bool LatisNum = double.TryParse(LatStr, out LatNum);

                if (!LatisNum)
                {
                    MessageBox.Show("Invalid number");
                    return;
                }

                _latitude = LatitudeBox.Text;
            }

            GPSUpdatedArgs arg = new GPSUpdatedArgs(_longitude, _latitude);
            GPSUpdatedEve(this,arg);
            
            this.Close();
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public event GPSUpdateHandler GPSUpdatedEve;
    }

    // add a delegate
    public delegate void GPSUpdateHandler(object sender, GPSUpdatedArgs e);
}
